import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  OneToOne,
  JoinColumn,
  OneToMany,
} from 'typeorm';
import { UserExtend } from './UserExtend';
import { Posts } from './Posts';

@Entity({ name: 'user' })
export class User {
  @PrimaryGeneratedColumn({
    // 设置主键自增
    type: 'int',
    name: 'id',
    comment: '主键ID', // 备注
  })
  id: number;

  @Column({
    type: 'varchar',
    nullable: false,
    length: 50,
    unique: true,
    name: 'username',
    comment: '用户名',
  })
  username: string;

  @Column({
    type: 'varchar',
    nullable: false,
    length: 100,
    name: 'password',
    default: () => 123456,
    comment: '密码',
  })
  password: string;
  // 是否删除
  @Column('tinyint', {
    nullable: false,
    default: () => 0,
    name: 'is_del',
    comment: '是否删除,1表示删除，0表示正常',
  })
  is_del: number;
  // 创建时间字段
  @CreateDateColumn({
    type: 'timestamp',
    nullable: false,
    name: 'created_at',
    comment: '创建时间',
  })
  createdAt: Date;
  // 更新时间字段
  @UpdateDateColumn({
    type: 'timestamp',
    nullable: false,
    name: 'updated_at',
    comment: '更新时间',
  })
  updateAt: Date;

  @Column()
  age: number;
  @OneToOne(
    type => UserExtend,
    userExtend => userExtend.user,
  )
  userDetail: UserExtend;
  // 关联posts表 post=>post.user 从post表中查询到user的数据
  @OneToMany(
    type => Posts,
    posts => posts.user,
  )
  posts: Posts[];
}
